<template>
  <div class="notification-container">
    <!-- 好友请求通知 -->
    <el-badge :value="pendingCount" :hidden="pendingCount === 0" class="friend-notification">
      <el-button 
        type="text" 
        @click="showNotifications = !showNotifications"
        class="notification-btn"
      >
        <el-icon><Bell /></el-icon>
      </el-button>
    </el-badge>
    
    <!-- 通知下拉面板 -->
    <div v-if="showNotifications" class="notification-panel">
      <div class="panel-header">
        <span>好友请求</span>
        <el-button type="text" @click="showNotifications = false">
          <el-icon><Close /></el-icon>
        </el-button>
      </div>
      
      <div class="notification-list">
        <div v-if="pendingRequests.length === 0" class="no-notifications">
          暂无新的好友请求
        </div>
        
        <div 
          v-for="request in pendingRequests" 
          :key="request.id" 
          class="notification-item"
        >
          <img 
            :src="request.avatar || '/images/default-avatar.png'" 
            :alt="request.username" 
            class="user-avatar"
          >
          <div class="notification-content">
            <div class="user-name">{{ request.username }}</div>
            <div class="request-message">{{ request.remark || '请求添加您为好友' }}</div>
            <div class="notification-actions">
              <el-button 
                size="small" 
                type="primary" 
                @click="handleRequest(request.id, true)"
              >
                同意
              </el-button>
              <el-button 
                size="small" 
                @click="handleRequest(request.id, false)"
              >
                拒绝
              </el-button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue';
import { Bell, Close } from '@element-plus/icons-vue';
import { ElMessage } from 'element-plus';
import axios from '@/utils/axiosConfig';

const showNotifications = ref(false);
const pendingRequests = ref([]);
const pendingCount = ref(0);

// 获取当前用户ID
const getCurrentUserId = () => {
  try {
    const token = localStorage.getItem('token');
    if (!token) return null;
    const payload = JSON.parse(atob(token.split('.')[1]));
    return payload.sub ? parseInt(payload.sub) : null;
  } catch (error) {
    return null;
  }
};

// 获取待确认的好友请求
const fetchPendingRequests = async () => {
  try {
    const response = await axios.get('/friends/requests/pending');
    if (response.data.success) {
      pendingRequests.value = response.data.data || [];
      pendingCount.value = pendingRequests.value.length;
    }
  } catch (error) {
    console.error('获取好友请求失败:', error);
  }
};

// 处理好友请求
const handleRequest = async (requestId, accept) => {
  try {
    const response = await axios.post('/friends/handle', {
      requestId,
      accept
    });
    if (response.data.success) {
      ElMessage.success(accept ? '已同意好友请求' : '已拒绝好友请求');
      // 操作成功后刷新好友请求列表
      await fetchPendingRequests();
      // 发送事件通知父组件更新好友列表
      if (accept) {
        window.dispatchEvent(new CustomEvent('friendRequestAccepted'));
      }
    } else {
      ElMessage.error(response.data.message || '操作失败');
    }
  } catch (error) {
    console.error('处理好友请求失败:', error);
    ElMessage.error('操作失败');
  }
};

// 初始化好友请求数据
const initFriendRequests = () => {
  const userId = getCurrentUserId();
  if (!userId) return;
  
  console.log('初始化好友请求数据，用户ID:', userId);
  fetchPendingRequests();
};

onMounted(() => {
  initFriendRequests();
});
</script>

<style scoped>
.notification-container {
  position: relative;
  display: inline-block;
}

.friend-notification {
  position: relative;
}

.notification-btn {
  padding: 12px;
  font-size: 20px;
  color: #606266;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  border-radius: 50%;
  transition: all 0.3s ease;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.notification-btn:hover {
  color: #409eff;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  transform: scale(1.1);
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
}

.friend-notification .el-badge__content {
  background-color: #ff4757;
  border: 2px solid #fff;
  font-weight: bold;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.1);
  }
  100% {
    transform: scale(1);
  }
}

.notification-panel {
  position: absolute;
  top: 100%;
  right: 0;
  width: 380px;
  background: white;
  border: 1px solid #e4e7ed;
  border-radius: 12px;
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
  z-index: 1000;
  max-height: 450px;
  overflow-y: auto;
  backdrop-filter: blur(10px);
  animation: slideDown 0.3s ease-out;
}

@keyframes slideDown {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid #e8eaed;
  font-weight: 700;
  color: #2c3e50;
  background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
  border-radius: 12px 12px 0 0;
  font-size: 16px;
}

.notification-list {
  max-height: 350px;
  overflow-y: auto;
  padding: 4px 0;
}

.no-notifications {
  padding: 40px 20px;
  text-align: center;
  color: #95a5a6;
  font-size: 15px;
  background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
  border-radius: 8px;
  margin: 16px;
  box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.1);
}

.notification-item {
  display: flex;
  padding: 16px;
  border-bottom: 1px solid #f0f2f5;
  transition: all 0.3s ease;
  border-radius: 8px;
  margin: 8px;
  background: linear-gradient(135deg, #fafbfc 0%, #f8f9fa 100%);
}

.notification-item:hover {
  background: linear-gradient(135deg, #e3f2fd 0%, #f3e5f5 100%);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.notification-item:last-child {
  border-bottom: none;
}

.user-avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  margin-right: 16px;
  object-fit: cover;
  border: 3px solid #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease;
}

.user-avatar:hover {
  transform: scale(1.1);
}

.notification-content {
  flex: 1;
}

.user-name {
  font-weight: 700;
  color: #2c3e50;
  margin-bottom: 6px;
  font-size: 16px;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.request-message {
  font-size: 13px;
  color: #7f8c8d;
  margin-bottom: 12px;
  line-height: 1.4;
}

.notification-actions {
  display: flex;
  gap: 10px;
}

.notification-actions .el-button {
  border-radius: 20px;
  font-weight: 600;
  transition: all 0.3s ease;
}

.notification-actions .el-button--primary {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
}

.notification-actions .el-button--primary:hover {
  background: linear-gradient(135deg, #5a6fd8 0%, #6a4190 100%);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
}

.notification-actions .el-button:not(.el-button--primary) {
  background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
  color: #6c757d;
  border: 1px solid #dee2e6;
}

.notification-actions .el-button:not(.el-button--primary):hover {
  background: linear-gradient(135deg, #e9ecef 0%, #dee2e6 100%);
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}
</style>